package com.kania.freestylewallparer;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener{
	TargetView imgTargetImage;
	
	Button btnSetWallpaper;
	
	CheckBox cbGradation;
	
	WallpaperManager mWm;
	
	Bitmap bitmap;
	
	static final int GALLERY = 1;
	
	//debug
	ImageView imgTest;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_view);
		
		imgTargetImage = (TargetView)findViewById(R.id.imgTarget);
		
		mWm = WallpaperManager.getInstance(MainActivity.this);
		
		
		btnSetWallpaper = (Button)findViewById(R.id.btnSetWallpaper);
		btnSetWallpaper.setOnClickListener(this);
		findViewById(R.id.btnColorPastelBlue).setOnClickListener(this);
		findViewById(R.id.btnColorPink).setOnClickListener(this);
		findViewById(R.id.btnColorDiamondGreen).setOnClickListener(this);
		findViewById(R.id.btnColorWhite).setOnClickListener(this);
		findViewById(R.id.btnColorBlack).setOnClickListener(this);
		cbGradation = (CheckBox)findViewById(R.id.cbGradation);
		cbGradation.setOnClickListener(this);
		
		
		
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_GET_CONTENT);
		intent.setType("image/*");
		//startActivityForResult(intent.createChooser(intent, "Select Picture"), GALLERY);
		startActivityForResult(intent, GALLERY);
	}
	
	void SetWallpaper(Bitmap bitmap) {
		try {
			mWm.setBitmap(bitmap);
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()) {
		case R.id.btnSetWallpaper:
			imgTargetImage.setDrawingCacheEnabled(true);
			
			Bitmap btmTest = imgTargetImage.getDrawingCache();
			if(btmTest == null) {
				Toast.makeText(MainActivity.this , "Bitmap is null!", Toast.LENGTH_SHORT).show();
			} else {
//				imgTest = (ImageView)findViewById(R.id.imgTest);
//				imgTest.setImageBitmap(btmTest);
				SetWallpaper(btmTest);
				
				finish();
			}
			break;
		case R.id.btnColorPastelBlue:
			imgTargetImage.setBGColor(0xffb2ccff);
			imgTargetImage.invalidate();
			break;
		case R.id.btnColorPink:
			imgTargetImage.setBGColor(0xffffcdff);
			imgTargetImage.invalidate();
			break;
		case R.id.btnColorDiamondGreen:
			imgTargetImage.setBGColor(0xffcefbc9);
			imgTargetImage.invalidate();
			break;
		case R.id.btnColorWhite:
			imgTargetImage.setBGColor(0xffffffff);
			imgTargetImage.invalidate();
			break;
		case R.id.btnColorBlack:
			imgTargetImage.setBGColor(0xff000000);
			imgTargetImage.invalidate();
			break;
		case R.id.cbGradation:
			if(cbGradation.isChecked())
				imgTargetImage.setGradation(true);
			else 
				imgTargetImage.setGradation(false);
			imgTargetImage.invalidate();
			break;
		}
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch(requestCode) {
		case GALLERY:
			if(requestCode == RESULT_CANCELED)
				return;
			if(data != null) {
				Uri imgUri = data.getData();
				try {
					bitmap = Images.Media.getBitmap(getContentResolver(), imgUri);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				imgTargetImage.setBitmap(bitmap);
			}
			break;
		}
	}
	
}
